﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using TVDL4.VO;
using TVDL4.BUS;
using C1.Win.C1FlexGrid;
namespace TVDL4
{
    public partial class FrmTimGiaiDoan : frmParent
    {
        private const string XML_FILE_PHUTRACH = "PhuTrachObject.xml";
        private const string XML_FILE_GIAIDOAN = "GiaiDoanObject.xml";
        private const string XML_FILE_THINGHIEMCHINH = "ThiNghiemChinhObject.xml";
        private const string XML_FILE_LOAICONGTRINH = "LoaiCongTrinhObject.xml";
        private GiaiDoanBUS GiaiDoanBUS;
        private List<GiaiDoanVO> lstGiaiDoanVO;
        public FrmTimGiaiDoan()
        {
            InitializeComponent();
        }

       

        private void FrmTimGiaiDoan_Load(object sender, EventArgs e)
        {
            GetPhuTrach();
            GetCongTrinh();
            GetGiaiDoan();
            LoadHoanThanh();
            GetLoaiCongTrinh();
            FormatFlex();
            GiaiDoanBUS = new GiaiDoanBUS();
            
        }
        #region "Combo"
        
        private void GetPhuTrach()
        {
            // Load the customer object from the existing XML file (if any)...
            if (File.Exists(XML_FILE_PHUTRACH) == true)
            {
                PhuTrach phutrach = ObjectXMLSerializer<PhuTrach>.Load(XML_FILE_PHUTRACH);
                this.LoadPhuTrachToCombo(phutrach);
            }

        }
        private void GetGiaiDoan()
        {
            // Load the customer object from the existing XML file (if any)...
            if (File.Exists(XML_FILE_GIAIDOAN) == true)
            {
                GiaiDoan giaidoan = ObjectXMLSerializer<GiaiDoan>.Load(XML_FILE_GIAIDOAN);
                this.LoadGiaiDoanToCombo(giaidoan);
            }

        }
        /// <summary>
        /// Load Nguyen quan len combo
        /// </summary>
        /// <param name="phutrach"></param>
        private void LoadPhuTrachToCombo(PhuTrach phutrach)
        {

            cboPhuTrach.ClearItems();
            cboPhuTrach.DefColWidth = 100;

            phutrach.Names.Sort();
            foreach (string Item in phutrach.Names)
            {
                cboPhuTrach.AddItem(Item);
            }
            cboPhuTrach.AllowSort = true;
            cboPhuTrach.Columns[0].Caption = "Name";
            cboPhuTrach.ColumnWidth = 250;


        }
        /// <summary>
        /// ADD nguyen quan vao file XML
        /// </summary>
        /// <returns></returns>
        private PhuTrach CreatePhuTrach()
        {
            PhuTrach phutrach = new PhuTrach();
            // Add nguyen quan as a collection of strings...
            phutrach.Names.Add(cboPhuTrach.Text);
            for (int i = 0; i < cboPhuTrach.ListCount; i++)
            {
                phutrach.Names.Add(cboPhuTrach.Columns[0].CellValue(i));
            }
            return phutrach;
        }

        private void GetCongTrinh()
        {
            CongTrinhBUS congtrinhBUS = new CongTrinhBUS();
            cboCongTrinh.ClearItems();
            cboCongTrinh.DefColWidth = 300;
            cboCongTrinh.AllowSort = true;
            cboCongTrinh.DataSource = congtrinhBUS.GetListCongTrinh();
            cboCongTrinh.ColumnWidth = 300;
          
        }
        /// <summary>
        /// Load Nguyen quan len combo
        /// </summary>
        /// <param name="giaidoan"></param>
        private void LoadGiaiDoanToCombo(GiaiDoan giaidoan)
        {

            cboGiaiDoan.ClearItems();
            cboGiaiDoan.DefColWidth = 100;

            giaidoan.Names.Sort();
            foreach (string Item in giaidoan.Names)
            {
                cboGiaiDoan.AddItem(Item);
            }
            cboGiaiDoan.AllowSort = true;
            cboGiaiDoan.Columns[0].Caption = "Name";
            cboGiaiDoan.ColumnWidth = 250;


        }
        /// <summary>
        /// ADD nguyen quan vao file XML
        /// </summary>
        /// <returns></returns>
        private GiaiDoan CreateGiaiDoan()
        {
            GiaiDoan giaidoan = new GiaiDoan();
            // Add nguyen quan as a collection of strings...
            giaidoan.Names.Add(cboCongTrinh.Text);
            for (int i = 0; i < cboCongTrinh.ListCount; i++)
            {
                giaidoan.Names.Add(cboCongTrinh.Columns[0].CellValue(i));
            }
            return giaidoan;
        }
        private void GetLoaiCongTrinh()
        {
            // Load the customer object from the existing XML file (if any)...
            if (File.Exists(XML_FILE_LOAICONGTRINH) == true)
            {
                LoaiCongTrinh loaicongtrinh = ObjectXMLSerializer<LoaiCongTrinh>.Load(XML_FILE_LOAICONGTRINH);
                this.LoadLoaiCongTrinhToCombo(loaicongtrinh);
            }

        }
        /// <summary>
        /// Load Nguyen quan len combo
        /// </summary>
        /// <param name="loaicongtrinh"></param>
        private void LoadLoaiCongTrinhToCombo(LoaiCongTrinh loaicongtrinh)
        {

            cboLoaiCongTrinh.ClearItems();
            cboLoaiCongTrinh.DefColWidth = 100;

            loaicongtrinh.Names.Sort();
            foreach (string Item in loaicongtrinh.Names)
            {
                cboLoaiCongTrinh.AddItem(Item);
            }
            cboLoaiCongTrinh.AllowSort = true;
            cboLoaiCongTrinh.Columns[0].Caption = "Name";
            cboLoaiCongTrinh.ColumnWidth = 250;


        }
        private void LoadHoanThanh()
        {

            cboHoanThanh.ClearItems();
            cboHoanThanh.DefColWidth = 100;
            cboHoanThanh.AddItem("Chưa hoàn thành");
            cboHoanThanh.AddItem("Hoàn thành");
            cboHoanThanh.AllowSort = true;
            cboHoanThanh.Columns[0].Caption = "Name";
            cboHoanThanh.ColumnWidth = 250;


        }
        #endregion
        private void FormatFlex()
        {
            flexGiaiDoan.Rows[0].AllowMerging = true;
            flexGiaiDoan.Rows[0].TextAlign = C1.Win.C1FlexGrid.TextAlignEnum.CenterCenter;
            flexGiaiDoan.Rows[1].TextAlign = C1.Win.C1FlexGrid.TextAlignEnum.CenterCenter;

            flexGiaiDoan.Rows.DefaultSize = 30;
            flexGiaiDoan.Rows[0].HeightDisplay = 40;
           
            
            //Mẫu Thực
            flexGiaiDoan.Cols["SLMTC"].Caption = "Số lượng mẫu \r\n tham chiểu";
            flexGiaiDoan.Cols["SLMT"].Caption = "Số lượng \r\n mẫu thực";
            flexGiaiDoan.Cols["NXThiNghiem"].Caption = "Năng suất \r\n thí Nghiệm";
            flexGiaiDoan.Cols["NXThamChieu"].Caption = "Năng suất \r\n tham chiếu";
            flexGiaiDoan.Cols["NSBQ"].Caption = "Năng suất \r\n bình quân";
            
        }
        private void btnXoa_Click(object sender, System.EventArgs e)
        {
            cboGiaiDoan.Text = "";
            cboPhuTrach.Text = "";
            dtTuNgay.Value = DateTime.Now;
            dtDenNgay.Value = DateTime.Now;
            dtTuNgay.Checked = false;
            dtDenNgay.Checked = false;
            cboCongTrinh.SelectedValue = "";
            cboLoaiCongTrinh.Text = "";
           
        }

        private void btnThoat_Click(object sender, System.EventArgs e)
        {
            this.Close();
        }

        private void btnTatCa_Click(object sender, System.EventArgs e)
        {
           
            lstGiaiDoanVO = GiaiDoanBUS.GetListGiaiDoan();
            flexGiaiDoan.Tree.Column = 2;
            flexGiaiDoan.Rows.Count = 1;
            int i = 1;
            foreach (GiaiDoanVO GiaiDoanVO in lstGiaiDoanVO)
            {

                //if (flexGiaiDoan.Rows[i - 1]["Congtrinh"].ToString() != GiaiDoanVO.CongTrinh.TenCongTrinh)
                //{
                //    flexGiaiDoan.Rows.InsertNode(i, 1);

                //}
                //else
                //{
                //    flexGiaiDoan.Rows.InsertNode(i, 2);
                //}
                //flexGiaiDoan.Rows.Count++;
                flexGiaiDoan.Rows.Count = flexGiaiDoan.Rows.Count + 1;
                flexGiaiDoan.Rows[i]["STT"] = i;
                flexGiaiDoan.Rows[i]["KEYSL"] = GiaiDoanVO.KeySL;
                flexGiaiDoan.Rows[i]["GiaiDoan"] = GiaiDoanVO.TenGiaiDoan;
                flexGiaiDoan.Rows[i]["CongTrinh"] = GiaiDoanVO.CongTrinh.TenCongTrinh;
                flexGiaiDoan.Rows[i]["LoaiCongTrinh"] = GiaiDoanVO.CongTrinh.LoaiCongTrinh;
                flexGiaiDoan.Rows[i]["PhuTrach"] = GiaiDoanVO.PhuTrach;
                flexGiaiDoan.Rows[i]["SLMTC"] = GiaiDoanVO.SlThamChieu;
                flexGiaiDoan.Rows[i]["SLMT"] = GiaiDoanVO.SlThiNghiem;
                flexGiaiDoan.Rows[i]["NXThiNghiem"] = GiaiDoanVO.NSThiNghiem;
                flexGiaiDoan.Rows[i]["NXThamChieu"] = GiaiDoanVO.NSThamChieu;
                flexGiaiDoan.Rows[i]["NSBQ"] = GiaiDoanVO.NSBQ;
                flexGiaiDoan.Rows[i]["TuNgay"] = GiaiDoanVO.ThoiGian;
                flexGiaiDoan.Rows[i]["DenNgay"] = GiaiDoanVO.ThoiGianDen;
                flexGiaiDoan.Rows[i]["HangMuc"] = GiaiDoanVO.Hangmuc;
                if (GiaiDoanVO.CongTrinh.HoanThanh == 1)
                {
                    C1.Win.C1FlexGrid.CellStyle rs = this.flexGiaiDoan.Styles.Add("rowcolor");
                    rs.ForeColor = Color.Blue;
                    flexGiaiDoan.Rows[i].Style = rs;
                }
                i++;
            }
        }

        private void flexGiaiDoan_DoubleClick(object sender, System.EventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;
            frmGiaiDoan childForm = GetForm("frmGiaiDoan");
            if (childForm == null)
            {
                childForm = new frmGiaiDoan();
                childForm.GiaiDoanVO = lstGiaiDoanVO.Find(
                delegate(GiaiDoanVO GiaiDoanVO)
                {
                    return GiaiDoanVO.KeySL == flexGiaiDoan.Rows[flexGiaiDoan.Selection.TopRow]["KEYSL"].ToString();
                }
                );
                childForm.MdiParent = this.MdiParent ;
                childForm.WindowState = FormWindowState.Maximized;
                childForm.Show();
                childForm.btnSua_Click(null, null);
                childForm.SetValueToControl();
                childForm.flexMauDat.Focus();
            }
            else
            {
                childForm.Activate();
            }
            Cursor.Current = Cursors.Default;
        }
        private frmGiaiDoan GetForm(String FormName)
        {
            foreach (frmGiaiDoan f in this.FindForm().MdiChildren)
            {
                if (f.Name == FormName)
                    return f;
            }
            return null;
        }

        private void btnTim_Click(object sender, EventArgs e)
        {
            string condition = "";
            if (cboGiaiDoan.Text != "")
            {
                condition = condition + " and TENGIAIDOAN = N'" + cboGiaiDoan.Text + "'";
            }
            if (cboCongTrinh.SelectedValue != null)
            {
                condition = condition + " and KEYSLCONGTRINH = '" + cboCongTrinh.SelectedValue.ToString() + "'";
            }
            if (txtHangMuc.Text != "")
            {
                condition = condition + " and HANGMUC like '%" + txtHangMuc.Text  + "%'";
            }
            if (cboLoaiCongTrinh.Text  != "")
            {
                condition = condition + " and LOAICONGTRINH = N'" + cboLoaiCongTrinh.Text + "'";
            }
            if (cboPhuTrach.Text != "")
            {
                condition = condition + " and PHUTRACH = N'" + cboPhuTrach.Text +"'";
            }
            if (dtTuNgay.Checked == true)
            {
                condition = condition + " and THOIGIAN >= '" + dtTuNgay.Value.Month.ToString() + "/" + dtTuNgay.Value.Day.ToString() + "/" + dtTuNgay.Value.Year.ToString() + "'";
            }
            if (dtDenNgay.Checked == true)
            {
                condition = condition + " and DENTHOIGIAN <= '" + dtDenNgay.Value.Month.ToString() + "/" + dtDenNgay.Value.Day.ToString() + "/" + dtDenNgay.Value.Year.ToString() + "'";
            }
            if (cboHoanThanh.Text == "Chưa hoàn thành")
            {
                condition = condition + " and HOANTHANH = 0 ";
            }
            else if (cboHoanThanh.Text == "Hoàn thành")
            {
                condition = condition + " and HOANTHANH = 1 ";
            }
            if (condition == "") { return; }

            lstGiaiDoanVO = GiaiDoanBUS.GetListGiaiDoan(condition);
            flexGiaiDoan.Tree.Column = 2;
            flexGiaiDoan.Rows.Count = 1;
            int i = 1;
            foreach (GiaiDoanVO GiaiDoanVO in lstGiaiDoanVO)
            {

                //if (flexGiaiDoan.Rows[i - 1]["Congtrinh"].ToString() != GiaiDoanVO.CongTrinh.TenCongTrinh)
                //{
                //    flexGiaiDoan.Rows.InsertNode(i, 1);

                //}
                //else
                //{
                //    flexGiaiDoan.Rows.InsertNode(i, 2);
                //}
                //flexGiaiDoan.Rows.Count++;
                flexGiaiDoan.Rows.Count = flexGiaiDoan.Rows.Count + 1;
                flexGiaiDoan.Rows[i]["STT"] = i;
                flexGiaiDoan.Rows[i]["KEYSL"] = GiaiDoanVO.KeySL;
                flexGiaiDoan.Rows[i]["GiaiDoan"] = GiaiDoanVO.TenGiaiDoan;
                flexGiaiDoan.Rows[i]["CongTrinh"] = GiaiDoanVO.CongTrinh.TenCongTrinh;
                flexGiaiDoan.Rows[i]["LoaiCongTrinh"] = GiaiDoanVO.CongTrinh.LoaiCongTrinh;
                flexGiaiDoan.Rows[i]["PhuTrach"] = GiaiDoanVO.PhuTrach;
                flexGiaiDoan.Rows[i]["SLMTC"] = GiaiDoanVO.SlThamChieu;
                flexGiaiDoan.Rows[i]["SLMT"] = GiaiDoanVO.SlThiNghiem;
                flexGiaiDoan.Rows[i]["NXThiNghiem"] = GiaiDoanVO.NSThiNghiem;
                flexGiaiDoan.Rows[i]["NXThamChieu"] = GiaiDoanVO.NSThamChieu;
                flexGiaiDoan.Rows[i]["NSBQ"] = GiaiDoanVO.NSBQ;
                flexGiaiDoan.Rows[i]["TuNgay"] = GiaiDoanVO.ThoiGian;
                flexGiaiDoan.Rows[i]["DenNgay"] = GiaiDoanVO.ThoiGianDen;
                flexGiaiDoan.Rows[i]["HangMuc"] = GiaiDoanVO.Hangmuc;
                if (GiaiDoanVO.CongTrinh.HoanThanh == 1)
                {
                    C1.Win.C1FlexGrid.CellStyle rs = this.flexGiaiDoan.Styles.Add("rowcolor");
                    rs.ForeColor = Color.Blue;
                    flexGiaiDoan.Rows[i].Style = rs;
                }
                i++;
            }
        }
    }
}
